package com.itboye.mengwu.app.ui.mymeng;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.login.ui.LoginActivity;
import com.itboye.mengwu.app.ui.aiwujia.YuErJingMomDetailActivity;
import com.itboye.mengwu.data.model.MePingLunbean;
import com.itboye.mengwu.data.presenter.MePingLunpter;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

//import com.itboye.core.imageview.RoundImage;

public class CommentMeActivity extends BaseActivity implements Observer,
		IXListViewListener {

	private Button btn_comment, btn_reply;
	private EditText edt_reply;
	private static final int ONE_COMMENT_CODE = -1;

	private PingLunchMeAdapter adapter;

	/** Item数据实体集合 */
	/** ListView对象 */
	private XListView mePingLun;
	private ImageView onPinZanback;
	MePingLunpter presenter;
	String uid;
	ArrayList<MePingLunbean.MyPingLun> arrlist = new ArrayList<MePingLunbean.MyPingLun>();
	MePingLunbean lunbean;
	private TextView txtMsg;
	private int pageNo = 1;
	RelativeLayout nodata;
	String authorId;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_pinglunme;
	}

	@Override
	public void onMyCreate() {
		txtMsg.setText("评论我的");
		adapter = new PingLunchMeAdapter(this, arrlist);
		mePingLun.setAdapter(adapter);
		mePingLun.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				if (arrlist.get(position - 1).getThread_info().getStatus()
						.equals("-1")) {
					ByAlert.alert("该贴已删除");
					return;
				}
				if (arrlist.get(position - 1).getThread_info().getFid()
						.equals("4")) {
					Intent intent = new Intent(CommentMeActivity.this,
							WoDeDongTaiDetail2.class);
					intent.putExtra("tid", arrlist.get(position - 1).getTid());
					startActivity(intent);
				} else if (arrlist.get(position - 1).getThread_info().getFid()
						.equals("6")
						|| arrlist.get(position - 1).getThread_info().getFid()
								.equals("7")
						|| arrlist.get(position - 1).getThread_info().getFid()
								.equals("8")) {// 育儿经
					Intent intent2 = new Intent(CommentMeActivity.this,
							YuErJingMomDetailActivity.class);
					intent2.putExtra("tid", arrlist.get(position - 1).getTid());
					startActivity(intent2);

				}
			}
		});
		initCommentData();

	}

	public boolean judgeLogin() {
		uid = SPUtils.get(this, null, SpPublicParams.UID, "") + "";
		if (uid == null || uid.equals("")) {

			Intent intent = new Intent(this, LoginActivity.class);
			startActivity(intent);
			return false;
		} else {
			return true;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == MePingLunpter.MePingLunSuccessEvent) {
				if (pageNo == 1)
					arrlist.clear();
				arrlist.addAll(((MePingLunbean) entity.getData()).getList());

				adapter.notifyDataSetChanged();
				// ByAlert.alert(entity.getData() + "");
				if (arrlist.size() <= 0) {
					nodata.setVisibility(View.VISIBLE);
					mePingLun.setVisibility(View.GONE);
				} else {
					nodata.setVisibility(View.GONE);
					mePingLun.setVisibility(View.VISIBLE);
				}
			}
		}
		stopLoad();
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception

		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		pageNo = 1;
		lunbean.setPageNo(pageNo + "");
		presenter.update(lunbean);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		pageNo++;
		lunbean.setPageNo(pageNo + "");
		presenter.update(lunbean);
	}

	public void stopLoad() {
		mePingLun.stopLoadMore();
		mePingLun.stopRefresh();
	}

	public void onPinZanback(View view) {
		this.finish();
	}

	public void initCommentData() {
		mePingLun.setXListViewListener(this);
		mePingLun.setPullLoadEnable(true);
		mePingLun.setPullRefreshEnable(true);
		showProgressDialog("数据加载中，请稍后", true);
		uid = SPUtils.get(CommentMeActivity.this, null, SpPublicParams.UID, "")
				+ "";
		presenter = new MePingLunpter(this);

		lunbean = new MePingLunbean();
		lunbean.setUid(uid);
		lunbean.setPageNo(pageNo + "");
		presenter.update(lunbean);
	}

	class PingLunchMeAdapter extends BaseAdapter {

		Context context;
		ArrayList<MePingLunbean.MyPingLun> list;

		public PingLunchMeAdapter(Context context,
				ArrayList<MePingLunbean.MyPingLun> list) {
			this.context = context;
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list == null ? 0 : list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHouder houder;
			if (convertView == null) {
				houder = new ViewHouder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_pinglunwode, null);
				houder.tv_title = (TextView) convertView
						.findViewById(R.id.tv_title);
				houder.textTime = (TextView) convertView
						.findViewById(R.id.textTime);
				houder.tv_content = (TextView) convertView
						.findViewById(R.id.tv_content);
				houder.iv_avatar = (ImageView) convertView
						.findViewById(R.id.iv_avatar);
				houder.txtPingLunTitle = (TextView) convertView
						.findViewById(R.id.txtPingLunTitle);
				houder.txtPinglunCountext = (TextView) convertView
						.findViewById(R.id.txtPinglunCountext);
				houder.tv_QuanMessge = (TextView) convertView
						.findViewById(R.id.tv_QuanMessge);
				houder.tetPing = (TextView) convertView
						.findViewById(R.id.tetPing);
				houder.txtcome = (TextView) convertView
						.findViewById(R.id.txtcome);
				houder.txtPinglunName = (TextView) convertView
						.findViewById(R.id.txtPinglunName);
				convertView.setTag(houder);

			} else {
				houder = (ViewHouder) convertView.getTag();
			}
			// XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
			// + list.get(position).getHead(), houder.iv_avatar);
			if (list.get(position).getHead() == null
					|| "".equals(list.get(position).getHead())) {
				if ("1".equals(list.get(position).getSex())) {
					// imageView.setImageResource(R.drawable.male);
					houder.iv_avatar.setBackgroundResource(R.drawable.male);
				} else {
					houder.iv_avatar.setImageResource(R.drawable.female);
				}
			} else {
				if (!list.get(position).getHead().startsWith("http")) {
					XImageLoader.load(GlobalConfig.API_URL
							+ "/picture/index?id="
							+ list.get(position).getHead(), houder.iv_avatar);
				} else {
					XImageLoader.load(list.get(position).getHead(),
							houder.iv_avatar);
				}
			}
			houder.tv_title.setText(list.get(position).getAuthor());
			houder.tv_content.setText(list.get(position).getMessage());
			houder.textTime
					.setText(new SimpleDateFormat("yyyy-MM-dd HH:ss")
							.format(Long.parseLong(list.get(position)
									.getDateline()) * 1000));
			houder.txtPingLunTitle.setText(list.get(position).getThread_info()
					.getNickname());
			houder.txtPinglunCountext.setText(list.get(position)
					.getThread_info().getMessage());
			houder.tv_QuanMessge.setText("评论了我");
			houder.tv_QuanMessge.setTextColor(R.color.gray);
			houder.txtPinglunName.setVisibility(View.GONE);
			houder.tetPing.setTag(R.id.tag_first, position);
			houder.tetPing.setTag(R.id.tag_second, position);
			if (list.get(position).getThread_info().getFid().equals("4")) {
				houder.txtcome.setText("宝宝乐");
			} else if (list.get(position).getThread_info().getFid().equals("5")) {
				houder.txtcome.setText("妈妈赠");
			} else if (list.get(position).getThread_info().getFid().equals("6")
					|| list.get(position).getThread_info().getFid().equals("7")
					|| list.get(position).getThread_info().getFid().equals("8")) {
				houder.txtcome.setText("育儿经");
			} else if (list.get(position).getThread_info().getFid().equals("9")) {
				houder.txtcome.setText("时光机");
			}
			return convertView;
		}

		class ViewHouder {
			public TextView txtPinglunName;
			private TextView tv_title;
			private TextView textTime;
			private TextView tv_content, txtcome;

			private ImageView iv_avatar;
			// 回复的
			private TextView txtPingLunTitle;
			private TextView txtPinglunCountext;

			private TextView tv_QuanMessge;
			private TextView tetPing;

		}
	}
}
